package model.commands.misc;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import model.Environment;
import model.commands.Command;

/**
 * Name: SAVE [ filename ] { cmd1 cmd2 ... cmdN }
 *
 * Description:
 * Saves the list of cmd1 cmd2 ... cmdN
 *
 * @author Erick Gonzalez
 */
public class SaveCommand extends Command {  
    private static final String COMMAND_LIBRARY_PATH = "CommandLibrary/";
    private String myFileName;
    private List<String> myCommandHistory;
    
    /**
     * 
     * @param fileName a fileName
     * @param commandHistory command history
     */
    public SaveCommand(String fileName, List<String> commandHistory) {
        myFileName = fileName;
        myCommandHistory = commandHistory;
    }
    
    @Override
    public int evaluate(Environment e) {
        try {
            FileWriter fstream = new FileWriter(COMMAND_LIBRARY_PATH + myFileName);
            BufferedWriter out = new BufferedWriter(fstream);
            for (String commandString : myCommandHistory) {
                out.write(commandString + "\n");
            }
            out.close();            
            return 1;
        } 
        catch (IOException e1) {
            return 0;
        }        
    }

}
